<?php

class BWMessage extends BWEntity {

    public $name = null;
    public $title;
    public $description;
    public $messageItems = array();
    protected $mediaManager = null;
    protected $messageManager = null;
    protected $inputMediaManager = null;
    protected $inputValueManager = null;
    protected $owner;
    public $ownerType;
    public $ownerId;

    public function addMessageItem($itemData, $data = NULL) {

        if (is_array($itemData)) {
            //get media type
            $mediaTypeId = $itemData['media_type'];
            $mediaType = new BWMediaGroup($mediaTypeId);
            //create new message item
            $messageItem = new BWMedia();
            $messageItem->name = $itemData['name'];
            $messageItem->content = $itemData['media_content'];
            $messageItem->save();
            //add messageItem type
            $mediaType->addMember($messageItem);
            //add messageItem into message
            $messagMediaModel = new BWMMessageMedia();
            $messagMediaModel->message_id = $this->getId();
            $messagMediaModel->media_id = $messageItem->getId();
            $messagMediaModel->save();
        }
    }

    public function getMessageManager($data = NULL) {
        if ($this->messageManager == NULL)
            $this->messageManager = new BWMessageManager($this);
        return $this->messageManager;
    }

    /**
     * 
     * @param type $data
     * @return BWMediaManager
     */
    public function getMediaManager($data = NULL) {

        if ($this->mediaManager == NULL) {
            $this->mediaManager = new BWMediaManager($this);
        }
        return $this->mediaManager;
    }

    public function getInputMediaManager($data = NULL) {
        if ($this->inputMediaManager == NULL) {
            $this->inputMediaManager = new BWInputMediaManager($this);
        }
        return $this->inputMediaManager;
    }

    public function getInputValueManager($data = NULL) {
        if ($this->inputValueManager == NULL) {
            $this->inputValueManager = new BWInputValue($this);
        }
        return $this->inputValueManager;
    }

    public function setOwner($owner) {
        if ($owner instanceof BEntity && $owner->getId() != NULL) {
            $this->ownerId = $owner->getId();
            $this->ownerType = get_class($owner);
        }
    }

    public function getRepository($data = NULL) {
        $repository = parent::getRepository($data);
        $repository->addPropertyToColumnMap('ownerType', 'resource_class');
        $repository->addPropertyToColumnMap('ownerId', 'resource_id');
        return $repository;
    }

    public static function findMessageList($data = NULL) {

        $message = new BWMessage();
        $messageList = array();

        $findSql = self::populateFindSql($data);

        //$fromSql .= $findCondition;

        if ($findSql != NULL) {
            $messageView = $message->getRepository()->getView(array('sql' => $findSql));

            $rows = $messageView->rows;

            foreach ($rows as $id => $row) {

                $bookableMessage = new BWMessage();
                $bookableMessage->getRepository()->loadEntity($row);
                $messageList[$id] = $bookableMessage;
            }
        }

        return $messageList;
    }

    public static function findMessageListCount($data = NULL) {
        $message = new BWMessage();

        $findSql = self::populateFindCountSql($data);

        //$fromSql .= $findCondition;

        if ($findSql != NULL) {
            return $message->getRepository()->getCount(array('sql' => $findSql));
        }

        return NULL;
    }

    protected static function populateFindSql($data = NULL) {
        $message = new BWMessage();

        $selectSql = 'select ms._id, ms.name, ms.description ';
        $fromSql = ' from tbl_message ms';
        $findCondition = " where ";
        $spaceId = Blyn::app()->getActiveSpace()->getId();


        if (is_array($data)) {

            if (isset($data['spaceId'])) {
                $spaceId = $data['spaceId'];
                unset($data['spaceId']);
            }

            //$findCondition .= 'ms.space_id = ' . $spaceId;

            if (isset($data['ownerData'])) {
                $ownerData = $data['ownerData'];

                if (is_array($ownerData) && $ownerData != NULL) {
                    $ownerType = 'BWMessage';
                    $ownerName = NULL;

                    if (isset($ownerData['ownerType'])) {
                        $ownerType = $ownerData['ownerType'];
                        unset($ownerData['ownerType']);
                    }

                    if (isset($ownerData['ownerName'])) {
                        $ownerName = $ownerData['ownerName'];
                        unset($ownerData['ownerName']);
                    }

                    $fromSql .= ",tbl_resource_message rs ";
                    $findCondition .= " and rs.resource_type ='" . $ownerType . "'";

                    if (isset($ownerData['findCondition'])) {
                        $fromSql .= ",tbl_message oms, tbl_resource_media ord, tbl_media omd ";
                        $findCondition .= $ownerData['findCondition'];
                    } else {
                        if ($ownerType == 'BWMessage' && $ownerData != NULL) {
                            //$selectSql .= ",oms.name, oms.description ";
                            $fromSql .= ",tbl_message oms, tbl_resource_media ord, tbl_media omd ";
                            $findCondition .= " and omd._id= ord.media_id and ord.resource_type = 'BWMessage' and ord.resource_id = oms._id "
                                    . "and rs.resource_type = 'BWMessage' and rs.resource_id = oms._id and rs.message_id = ms._id ";
                            foreach ($ownerData as $name => $value) {

                                if (trim($findCondition) == 'where')
                                    $findCondition .= " omd.name='" . $name . "' and omd.media_content='" . $value . "'";
                                else
                                    $findCondition .= " and omd.name='" . $name . "' and omd.media_content='" . $value . "'";
                            }
                        }
                    }
                }

                unset($data['ownerData']);
            }

            if (isset($data['mediaData'])) {
                $mediaData = $data['mediaData'];

                if (isset($mediaData['findCondition']))
                    $findCondition .= $mediaData['findCondition'];
                else {
                    if (is_array($mediaData)) {
                        $i = 1;
                        foreach ($mediaData as $name => $value) {
                            $fromSql .= ", tbl_media md" . $i . ", tbl_resource_media rd" . $i;
                            $selectSql .= ",md" . $i . ".name, md" . $i . ".media_content";
                            if (trim($findCondition) == 'where')
                                $findCondition .=" rd" . $i . ".space_id=" . $spaceId . " and rd" . $i . ".resource_id=ms._id " . " and rd" . $i . ".resource_type='BWMessage'" . " and rd" . $i . ".media_id=" . "md" . $i . "._id " . " and md" . $i . ".name='" . $name . "' and md" . $i . ".media_content = '" . $value . "'";
                            else
                                $findCondition .=" and rd" . $i . ".space_id=" . $spaceId . " and rd" . $i . ".resource_id=ms._id" . " and rd" . $i . ".resource_type='BWMessage'" . " and rd" . $i . ".media_id=" . "md" . $i . "._id " . " and md" . $i . ".name='" . $name . "' and md" . $i . ".media_content = '" . $value . "'";
                            $i++;
                        }
                    }
                }

                unset($data['mediaData']);
            }

            if (isset($data['findCondition'])) {
                $findCondition .= " and ms." . $data['findCondition'];
            } else {
                $findContext = $message->getRepository()->populateAttributeList($data);

                foreach ($findContext as $name => $value) {
                    //$selectSql .= ",ms.name,ms.description";
                    $findCondition = " and ms." . $name . " = '" . $value . "'";
                }
            }

            $findSql = $selectSql . $fromSql . $findCondition . ' group by ms._id';

            return $findSql;
        }
    }

    protected static function populateFindCountSql($data = NULL) {
        $message = new BWMessage();

        $selectSql = 'select count(ms._id) ';
        $fromSql = ' from tbl_message ms';
        $findCondition = " where ";
        $spaceId = Blyn::app()->getActiveSpace()->getId();


        if (is_array($data)) {

            if (isset($data['spaceId'])) {
                $spaceId = $data['spaceId'];
                unset($data['spaceId']);
            }

            //$findCondition .= 'ms.space_id = ' . $spaceId;

            if (isset($data['ownerData'])) {
                $ownerData = $data['ownerData'];

                if (is_array($ownerData) && $ownerData != NULL) {
                    $ownerType = 'BWMessage';
                    $ownerName = NULL;

                    if (isset($ownerData['ownerType'])) {
                        $ownerType = $ownerData['ownerType'];
                        unset($ownerData['ownerType']);
                    }

                    if (isset($ownerData['ownerName'])) {
                        $ownerName = $ownerData['ownerName'];
                        unset($ownerData['ownerName']);
                    }

                    $fromSql .= ",tbl_resource_message rs ";
                    $findCondition .= " and rs.resource_type ='" . $ownerType . "'";

                    if (isset($ownerData['findCondition'])) {
                        $fromSql .= ",tbl_message oms, tbl_resource_media ord, tbl_media omd ";
                        $findCondition .= $ownerData['findCondition'];
                    } else {
                        if ($ownerType == 'BWMessage' && $ownerData != NULL) {
                            //$selectSql .= ",oms.name, oms.description ";
                            $fromSql .= ",tbl_message oms, tbl_resource_media ord, tbl_media omd ";
                            $findCondition .= " and omd._id= ord.media_id and ord.resource_type = 'BWMessage' and ord.resource_id = oms._id "
                                    . "and rs.resource_type = 'BWMessage' and rs.resource_id = oms._id and rs.message_id = ms._id ";
                            foreach ($ownerData as $name => $value) {
                                //$selectSql .= ",omd.name,omd.media_content";
                                if (trim($findCondition) == 'where')
                                    $findCondition .= " omd.name='" . $name . "' and omd.media_content='" . $value . "'";
                                else
                                    $findCondition .= " and omd.name='" . $name . "' and omd.media_content='" . $value . "'";
                            }
                        }
                    }
                }

                unset($data['ownerData']);
            }

            if (isset($data['mediaData'])) {
                $mediaData = $data['mediaData'];

                if (isset($mediaData['findCondition']))
                    $findCondition .= $mediaData['findCondition'];
                else {
                    if (is_array($mediaData)) {
                        $i = 1;
                        foreach ($mediaData as $name => $value) {
                            $fromSql .= ", tbl_media md" . $i . ", tbl_resource_media rd" . $i;
                            //$selectSql .= ",md.name, md.media_content";
                            if (trim($findCondition) == 'where')
                                $findCondition .=" rd" . $i . ".space_id=" . $spaceId . " and rd" . $i . ".resource_id=ms._id " . " and rd" . $i . ".resource_type='BWMessage'" . " and rd" . $i . ".media_id=" . "md" . $i . "._id " . " and md" . $i . ".name='" . $name . "' and md" . $i . ".media_content = '" . $value . "'";
                            else
                                $findCondition .= " and rd" . $i . ".space_id=" . $spaceId . " and rd" . $i . ".resource_id=ms._id" . " and rd" . $i . ".resource_type='BWMessage'" . " and rd" . $i . ".media_id=" . "md" . $i . "._id " . " and md" . $i . ".name='" . $name . "' and md" . $i . ".media_content = '" . $value . "'";
                            $i++;
                        }
                    }
                }

                unset($data['mediaData']);
            }

            if (isset($data['findCondition'])) {
                $findCondition .= " and ms." . $data['findCondition'];
            } else {
                $findContext = $message->getRepository()->populateAttributeList($data);

                foreach ($findContext as $name => $value) {
                    //$selectSql .= ",ms.name,ms.description";
                    $findCondition = " and ms." . $name . " = '" . $value . "'";
                }
            }

            $findSql = $selectSql . $fromSql . $findCondition;

            return $findSql;
        }
    }

}

?>
